﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace HousingDistribution
{
    partial class FormPersonDetailsTab
    {
        private void InitDGVPersonSearch()
        {
            //规定不自动生成列
            this.dataGridViewPersonSearch.AutoGenerateColumns = false;

            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ID), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.ID));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.NAME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TYPE));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.SEX));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.UNIT));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.ADMIN_DUTY));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.ADMIN_DUTY_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TECH_RANK));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TECH_RANK_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TECH_TITLE));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TECH_TITLE_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.CUR_RANK_BEFORE_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.CUR_RANK_BEFORE_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.WORK_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SCHOOL_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.SCHOOL_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SIGNUP_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.SIGNUP_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_NAME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.MATE_NAME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_UNIT), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.MATE_UNIT));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_RANK), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.MATE_RANK));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_ELIGIBLE), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.HOUSING_ELIGIBLE));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.REMARKS), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.REMARKS));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.WORK_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.RETIREMENT_TIME), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.RETIREMENT_TIME));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SERVICE_LENGTH), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.SERVICE_LENGTH));
            this.dataGridViewPersonSearch.Columns.Add(DataDictionary.Instance().PersonTableFieldName(PersonColConsts.EVALUATION), DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.EVALUATION));


            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ID)].Visible = false;
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME)].HeaderText = DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.NAME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE)].HeaderText = DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.TYPE);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX)].HeaderText = DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.SEX);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT)].HeaderText = DataDictionary.Instance().PersonTableFieldTitle(PersonColConsts.UNIT);
            
            for (int i=5; i<this.dataGridViewPersonSearch.Columns.Count; i++)
            {
                this.dataGridViewPersonSearch.Columns[i].Visible = false;
            }

            //bind data
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ID)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ID);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.CUR_RANK_BEFORE_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.CUR_RANK_BEFORE_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SCHOOL_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SCHOOL_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SIGNUP_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SIGNUP_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_NAME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_NAME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_UNIT)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_UNIT);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_RANK)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_RANK);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_ELIGIBLE)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_ELIGIBLE);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.REMARKS)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.REMARKS);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.RETIREMENT_TIME)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.RETIREMENT_TIME);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SERVICE_LENGTH)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SERVICE_LENGTH);
            this.dataGridViewPersonSearch.Columns[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.EVALUATION)].DataPropertyName = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.EVALUATION);
        }

        private void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Only Accept a char, or backspace
            if ((Char.IsLetter(e.KeyChar) || e.KeyChar == 8))
            {
                //If we are not overwriting the whole text
                if (this.textBoxSearch.SelectionLength != this.textBoxSearch.Text.Length)
                {
                    //Check that the new Text value will be a valid
                    // ip octet then move on to next box
                    if (e.KeyChar == 8)
                    {
                        Query(this.textBoxSearch.Text.Remove(this.textBoxSearch.Text.Length - 1, 1));
                    }
                    else if (this.textBoxSearch.Text.Length == 3)
                    {
                        this.textBoxSearch.SelectAll();
                        e.Handled = true;
                    }
                    else
                    {
                        Query(this.textBoxSearch.Text + e.KeyChar.ToString());
                    }
                }//首字母
                else if (this.textBoxSearch.Text.Length == 0)
                {
                    Query(e.KeyChar.ToString());
                }
            }
            //Do nothing if the keypress is not a char, or SPLIT_STR
            else
                e.Handled = true;
        }

        private void Query(string name)
        {
            if (name.Length > 0)
            {
                StringBuilder sql = new StringBuilder(PERSON_QUERY_SQL_HEADER);
                sql.Append(GetNameInitialQueryString());
                sql.Append("order by idperson desc limit 0, ");
                sql.Append(MAX_COUNT.ToString());

                //重新设置读取数据库中总人数（该条件下）
                this.personCount = GetPersonCount();

                this.personTable = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                this.dataGridViewPersonSearch.DataSource = this.personTable;
            }
            else
                this.dataGridViewPersonSearch.DataSource = null;
        }


        private void dataGridViewPersonSearch_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            if (rowIndex < 0 || colIndex < 0)
                return;

            this.curIndex = rowIndex;

            DisplayInfo(this.curIndex);

            RefreshHousingDisState(this.curPerInfo);
        }

        private string GetNameInitialQueryString()
        {
            if (this.textBoxSearch.Text.Length == 0)
                return "and 1=1 ";

            StringBuilder sql = new StringBuilder("and person.nameInitial like '");
            sql.Append(this.textBoxSearch.Text);
            sql.Append("%' ");

            return sql.ToString();
        }
    }
}
